package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
///admin/product/{page}/{limit}
public class SpuManageController {
    @Autowired
    private ManageService manageService;

    @GetMapping("{page}/{limit}")
// 对象属性传值 springmvc中如果页面提交过来参数和实体类的参数一直 可以用实体类参数接收数据
    public Result getSpuInfoPage(@PathVariable Long page,
                                 @PathVariable Long limit,
                                 SpuInfo spuInfo) {
        Page<SpuInfo> spuInfoPage = new Page<>(page, limit);
        //调用服务层
        //Ipage是mybatis-plus中提供的分页方法
        IPage iPage = manageService.getSpuInfoPage(spuInfoPage, spuInfo);
        return Result.ok(iPage);
    }

    // /admin/product/baseSaleAttrList
    @GetMapping("baseSaleAttrList")
    public Result baseSaleAttrList() {
        List<BaseSaleAttr> baseSaleAttrs = manageService.getBaseSaleAttrList();
        return Result.ok(baseSaleAttrs);
    }

    //  http://localhost/admin/product/saveSpuInfo
    //  Json ---> Java Object
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo) {
        //  保存方法！
        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    ///admin/product/spuImageList/{spuId}
    @GetMapping("/spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable Long spuId) {
        List<SpuImage> spuImageList = manageService.getSpuImageList(spuId);
        return Result.ok(spuImageList);
    }

    @GetMapping("/spuSaleAttrList/{spuId}")///admin/product/spuSaleAttrList/{spuId}
    public Result getSpuSaleAttrList(@PathVariable Long spuId) {
       List<SpuSaleAttr> spuSaleAttrList= manageService.getSpuSaleAttrList(spuId);
       return Result.ok(spuSaleAttrList);
    }
}
